/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-6 上午10:27
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.MyCollection;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dqhl on 17/7/6.
 */
public class MyCollectionAdapter extends RecyclerView.Adapter<MyCollectionAdapter.MyCollectionViewHolder> {
    private Context mContext;
    private List<MyCollection.DataBean> mCollectionList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private onItemClickListener onItemClickListener;

    public MyCollectionAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void refresh_info(List<MyCollection.DataBean> collectionList) {
        this.mCollectionList = collectionList;
        notifyDataSetChanged();
    }


    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

    }
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public MyCollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyCollectionViewHolder viewHolder = new MyCollectionViewHolder(mLayoutInflater.inflate(R.layout.layout_mycollection_item, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyCollectionViewHolder holder, int position) {
        holder.tv_content.setText(mCollectionList.get(position).getTitle());
        // 类型 a直播 b资讯 c生活
        if ("a".equals(mCollectionList.get(position).getTheme_type())) {
            holder.tv_title.setText("直播");
        } else if ("b".equals(mCollectionList.get(position).getTheme_type())) {
            holder.tv_title.setText("资讯");
        } else {
            holder.tv_title.setText("生活");
        }

        if(onItemClickListener != null){
            holder.ll_collectionAdapterItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mCollectionList.size();
    }

    class MyCollectionViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_collectionAdapterItem;
        TextView tv_title;
        TextView tv_content;

        public MyCollectionViewHolder(View itemView) {
            super(itemView);
            ll_collectionAdapterItem = (LinearLayout) itemView.findViewById(R.id.ll_collectionAdapterItem);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }
}
